package storage;

/**
 *@author Adam
 */


import java.nio.CharBuffer;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class TextBasedStorage implements Storage{

	RMIServerImpl rmi;
	ConnectSocket SSS;
	BonjourService bs;
	
	public CharBuffer buff;

	public TextBasedStorage() {
	}

	public int storageNumbers(){
		int number = 1;
		try{
			Registry registry = LocateRegistry.getRegistry();
			String[] registryComponents = registry.list();
			for (int i = 0; i < registryComponents.length; i++){
				if(registryComponents[i].startsWith("storage")) 
					number++;
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return number;
	}


	public void begin(){
		try{
			int num = storageNumbers();
			rmi = new RMIServerImpl("storage"+num);
			rmi.begin();
			SSS = new ConnectSocket(1270, "storage"+num,"none");
			int port = SSS.getPort();
			bs = new BonjourService("storage"+num, port);
			bs.registerService();
			SSS.listen();
			//SSS.close();

		} 
		catch(Exception e){
			System.out.println("problem");
			e.printStackTrace();
		}
	}


	public static void main(String[] args){
		TextBasedStorage ts = new TextBasedStorage();
		ts.begin();
	}
}